//
//  DetailPersonViewController.m
//  homeWork-18
//
//  Created by 鹏飞罗 on 16/8/8.
//  Copyright © 2016年 罗鹏飞. All rights reserved.
//

#import "DetailPersonViewController.h"
#import "SQLiteManager.h"
#import "Person.h"
@interface DetailPersonViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic, strong) UIImagePickerController *picker;
@property (nonatomic, strong) SQLiteManager *dataBaseManage;
@end

@implementation DetailPersonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"详情页面";
    
    _personImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [_personImage addGestureRecognizer:tap];
    /** 用UIImagePickerController 类来获取图片视频，大体分为以下几个步骤：
     
     1. 初始化UIImagePickerController 类；
     
     2. 设置UIImagePickerController 实例的数据来源类型（下面解释）；
     
     3. 设置设置代理；
     
     4. 如果需要做图片修改的话设置allowsEditing =yes。
     */
    //1.初始化 UIImagePickerController
    _picker = [[UIImagePickerController alloc] init];
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    //2.设置UIImagePickerController 实例的数据来源类型
    _picker.sourceType = sourceType;
    //   3. 设置设置代理
    _picker.delegate = self;
    //    4.如果需要做图片修改的话设置
    //    _picker.allowsEditing = YES;
    _dataBaseManage = [SQLiteManager sharedManger];
}

- (void)tapAction:(UITapGestureRecognizer *)tap {
    [self presentViewController:_picker animated:YES completion:nil];
}
// 当用户选取完成时
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    _personImage.image = image;
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}
// 用户点击取消时退出picker
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (IBAction)keepButtonClicked:(id)sender {
    
    
    [_dataBaseManage openSQLite3];
    [_dataBaseManage cerateTable];
    
    Person *person = [Person new];
    
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *personImagePath = [[pathArray firstObject] stringByAppendingPathComponent:@"personImage/personImageData"];
    NSLog(@"%@",personImagePath);
    NSData *imageData = UIImagePNGRepresentation(_personImage.image);
   BOOL issuccess = [imageData writeToFile:personImagePath atomically:YES];
    if (issuccess) {
        NSLog(@"写入图片路径成功");
    } else {
        NSLog(@"写入图片路径失败");
    }
    
    person.personImagePath = @"personImage/zhangsanPersonImageData";
    person.name = _name.text;
    person.phoneNumber = _phoneNumber.text;
    person.address = _address.text;
    [_dataBaseManage insertPerson:person];
    [_dataBaseManage closeSQLite3];
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
